package com.elitedatai.mchtest.wayline.repository;

import com.elitedatai.mchtest.wayline.model.entity.WaylineJobEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.Query;

import java.util.Collection;
import java.util.List;

public interface WaylineJobRepository extends JpaRepository<WaylineJobEntity, Long> {
    WaylineJobEntity findByWorkspaceIdAndJobId(String workspaceId, String jobId);

    @Query("SELECT w FROM WaylineJobEntity w WHERE w.workspaceId = :workspaceId ORDER BY w.id DESC")
    Page<WaylineJobEntity> findByWorkspaceIdOrderByDesc(@Param("workspaceId") String workspaceId, Pageable pageable);

    @Query("SELECT w FROM WaylineJobEntity w WHERE w.workspaceId = :workspaceId " +
            "AND (:status IS NULL OR w.status = :status) " +
            "AND (:jobIds IS NULL OR w.jobId IN :jobIds)")
    List<WaylineJobEntity> findByWorkspaceIdAndJobIdsAndStatus(
            @Param("workspaceId") String workspaceId,
            @Param("jobIds") Collection<String> jobIds,
            @Param("status") Integer status);
}
